package com.doophan.duuchin.service.impl;

import com.doophan.duuchin.dto.PageDTO;
import com.doophan.duuchin.dto.QueryDTO;
import com.doophan.duuchin.mapper.UserMapper;
import com.doophan.duuchin.mapper.VideoMapper;
import com.doophan.duuchin.model.Video;
import com.doophan.duuchin.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Resource
    private VideoMapper videoMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public PageDTO<Video> getList(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<Video> videoList = videoMapper.selectList(null);
        videoList.forEach( e -> e.setUser(userMapper.selectById(e.getUserId())));
        PageInfo pageInfo = new PageInfo<>(videoList);
        PageHelper.clearPage();
        PageDTO pageDTO = PageDTO.builder().total(pageInfo.getTotal()).data(pageInfo.getList()).build();
        return pageDTO;
    }

    @Override
    public Video getInfo(Integer id) {
        return videoMapper.selectById(id);
    }
}
